package helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * This is the helper class, responsible for generating messages, signals and transformations
 * between different formats (e.g. from string to List of Binary Numbers etc.)
 * Created by Eugene Khoroshutin
 * Date: 12/29/12
 * Time: 11:18 PM
 */
public class MessageAndSignalHandler {
    /**
     * Generates String of specified length, which consists of pseudo-random bits
     * @param length length of the string
     * @return resulting string
     */
    public static String generateRandomBitsString(int length) {
        String result = "";
        Random r = new Random();
        for (int i = 0; i < length; i++) {
            Boolean rand = r.nextBoolean();
            if (rand) {
                result += 1;
            } else {
                result += 0;
            }
        }
        return result;
    }

    /**
     * !!!Attention. For data integrity make sure that input string length is divisible by binaryNumberLength
     * Transforms the String to ArrayList of Binary Numbers, with specified Binary Number length
     * @param input
     * @param binaryNumberLength
     * @return
     */
    public static List<BinaryNumber> stringToBinaryNumbers(String input, int binaryNumberLength){
        List<BinaryNumber> result=new ArrayList<BinaryNumber>();
        int counter = 0;
        int binaryNumbers = input.length() / binaryNumberLength;
        for (int i = 0; i < binaryNumbers; i++) {
            String current = input.substring(counter, counter + binaryNumberLength);
            counter += binaryNumberLength;
            BinaryNumber first = new BinaryNumber(current);
            result.add(first);
        }
        return result;
    }
}
